package com.dm.cloud.images.mode;


import com.github.dockerjava.api.model.Network;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description ="Docker网络模型")
@Data
public class NetworkDetail {
    @Schema(description = "网络ID")
    private String id;
    @Schema(description = "网络在主机内的名称")
    private String name;
    @Schema(description = "域")
    private String scope;
    @Schema(description = "驱动")
    private String driver;
    @Schema(description = "是否显示IPV6")
    private Boolean enableIPv6;
    @Schema(description = "。。。。")
    private Boolean internal;
    @Schema(description = "。。。。")
    private Network.Ipam ipam;
    @Schema(description = "。。。。")
    private Map<String, Network.ContainerNetworkConfig> containers;
    @Schema(description = "配置参数")
    private Map<String, String> options;

    private Boolean attachable;
    @Schema(description = "标签")
    public Map<String, String> labels;

    @Schema(description = "节点ID")
    private String nodeId;

    @Schema(description = "自定义节点名称")
    private String nodeName;

    @Schema(description = "自定义节点IP")
    private String nodeHost;

    /**
     * 根据docker的网络结构复制
     * @param networks
     * @param name
     * @param host
     * @return
     */
    public static List<NetworkDetail> buildByDockerNetworkList(List<Network> networks,String nodeId, String name, String host) {
        List<NetworkDetail> nts=new ArrayList<>();
        if(networks!=null && !networks.isEmpty()){
            for (Network network : networks) {
                nts.add(NetworkDetail
                        .builder()
                        .ipam(network.getIpam())
                        .id(network.getId())
                        .attachable(network.isAttachable())
                        .name(network.getName())
                        .driver(network.getDriver())
                        .containers(network.getContainers())
                        .scope(network.getScope())
                        .enableIPv6(network.getEnableIPv6())
                        .internal(network.getInternal())
                        .options(network.getOptions())
                        .labels(network.getLabels())
                        .nodeId(nodeId)
                        .nodeName(name)
                        .nodeHost(host)
                        .build());
            }
        }
        return nts;
    }


    public static NetworkDetail buildByDockerNetwork(Network network,String nodeId, String nodeName, String nodeHost) {
                return NetworkDetail
                        .builder()
                        .ipam(network.getIpam())
                        .id(network.getId())
                        .attachable(network.isAttachable())
                        .name(network.getName())
                        .driver(network.getDriver())
                        .containers(network.getContainers())
                        .scope(network.getScope())
                        .enableIPv6(network.getEnableIPv6())
                        .internal(network.getInternal())
                        .options(network.getOptions())
                        .labels(network.getLabels())
                        .nodeId(nodeId)
                        .nodeName(nodeName)
                        .nodeHost(nodeHost)
                        .build();

    }
}
